package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/8 20:59
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.server.po.*;
import net.chenyan.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/8 20:59
 */
@Slf4j
@Api(tags = {"用户音乐接口"})
@RestController
@RequestMapping(value = "/admapi/userMusic")
public class UserMusicController {
    @Autowired
    private UserMusicService userMusicService;
    @Autowired
    private UserService userService;
    @Autowired
    private MusicService musicService;

    @ApiOperation(value = "用户点赞/取消点赞音乐")
    @PostMapping(value = "/likeMusic")
    public R likeMusic(@RequestBody int userId, int musicId) {
        User user=userService.getById(userId);
        Music music=musicService.getById(musicId);
        if(user==null || music==null){
            return R.error("用户id或者音乐id不存在！");
        }
        int likeNum=music.getLikedNum();
        QueryWrapper<UserMusic> userMusicQueryWrapper=new QueryWrapper<>();
        userMusicQueryWrapper.eq("user_id",userId);
        userMusicQueryWrapper.eq("music_id",musicId);
        UserMusic userMusic=userMusicService.getOne(userMusicQueryWrapper);
        if(userMusic==null){
            UserMusic userMusic1=new UserMusic();
            userMusic1.setMusicId(musicId);
            userMusic1.setUserId(userId);
            userMusic1.setIsLike(1);
            userMusicService.save(userMusic1);
            music.setLikedNum(likeNum+1);
            musicService.updateById(music);
            return R.ok("点赞成功");
        }else{
            if(userMusic.getIsLike()==1){
                userMusic.setIsLike(0);
                music.setLikedNum(likeNum-1);
                musicService.updateById(music);
                userMusicService.updateById(userMusic);
                return R.ok("取消点赞成功");
            }else{
                userMusic.setIsLike(1);
                music.setLikedNum(likeNum+1);
                musicService.updateById(music);
                userMusicService.updateById(userMusic);
                return R.ok("点赞成功");
            }

        }

    }

    @ApiOperation(value = "用户收藏/取消收藏音乐")
    @PostMapping(value = "/collectMusic")
    public R collectMusic(@RequestBody int userId,int musicId) {
        User user=userService.getById(userId);
        Music music=musicService.getById(musicId);
        if(user==null || music==null){
            return R.error("用户id或者专辑id不存在！");
        }
        QueryWrapper<UserMusic> userMusicQueryWrapper=new QueryWrapper<>();
        userMusicQueryWrapper.eq("user_id",userId);
        userMusicQueryWrapper.eq("music_id",musicId);
        UserMusic userMusic=userMusicService.getOne(userMusicQueryWrapper);
        if(userMusic==null){
            UserMusic userMusic1=new UserMusic();
            userMusic1.setMusicId(musicId);
            userMusic1.setUserId(userId);
            userMusic1.setIsCollect(1);
            userMusicService.save(userMusic1);
            return R.ok("收藏成功");
        }else{
            if(userMusic.getIsCollect()==1){
                userMusic.setIsCollect(0);
            }else{
                userMusic.setIsCollect(1);
            }
            boolean temp = userMusicService.updateById(userMusic);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }

    }

    @ApiOperation(value = "用户评论歌曲")
    @PostMapping(value = "/commentMusic")
    public R commentMusic(@RequestBody int userId,int musicId,String comment) {
        User user=userService.getById(userId);
        Music music=musicService.getById(musicId);
        if(user==null || music==null){
            return R.error("用户id或者专辑id不存在！");
        }
        QueryWrapper<UserMusic> userMusicQueryWrapper=new QueryWrapper<>();
        userMusicQueryWrapper.eq("user_id",userId);
        userMusicQueryWrapper.eq("music_id",musicId);
        UserMusic userMusic=userMusicService.getOne(userMusicQueryWrapper);
        if(userMusic==null){
            UserMusic userMusic1=new UserMusic();
            userMusic1.setMusicId(musicId);
            userMusic1.setUserId(userId);
            userMusic1.setComment(comment);
            userMusicService.save(userMusic1);
            return R.ok("评论成功");
        }else{
            userMusic.setComment(comment);
            boolean temp = userMusicService.updateById(userMusic);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }
    }

    @ApiOperation(value = "获取用户收藏的音乐")
    @PostMapping(value = "/getMusicByUserId")
    public R getMusicByUserId(int userId, @RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(musicService.findAllMusicByUserId(PageRequest.of(page,size),userId));
    }
}
